// pages/mch/finstat/index.js
const App = getApp()
import Kits from '../../../utils/kits'
import Util from '../../../utils/util'

Page({
  data: {
    init: false,

    summ: {
      couponStr: '--',
      amountStr: '--',
      settleStr: '--',
      depositStr: '--',
    },

    show: false,
    qr: '',
    pid: '',
    pamt: '',
    pcnl: '',

    listIndex: 0,
    list: [],
    listShow: false,
    tab: 0,

    // income
    loading: false,
    triggered: true,
    CustomBar: App.Global.CustomBar,

    begin: new Date().addDays(-7).format('yyyy-MM-dd'),
    close: new Date().format('yyyy-MM-dd'),
    today: new Date().format('yyyy-MM-dd'),

    page: 1,
    size: 8,
    time: new Date().getTime(),
    pager: {
      now: 1,
      row: 0,
      all: 0,
      fms: 0,
    },
    items: []
  },
  changeTab(e) {
    this.setData({
      tab: e.currentTarget.dataset.tab
    })
  },
  choose(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      listIndex: index
    })
  },
  showList() {
    this.setData({
      listShow: !this.data.listShow
    })
  },
  getData() {
    App.doPost({
      uri: '/app/merchant/contract/price/list',
      fnSucc: (res) => {
        this.setData({
          list: res.data.items
        })
        console.log(res);
      }
    })
  },
  // 购买
  payCount() {
    App.doPost({
      uri: '/app/merchant/contract/pay',
      body: {
        type: 'Contract',
        data: String(this.data.list[this.data.listIndex].ID),
        pcnl: 21,
        parg: JSON.stringify({
          appid: App.Global.AppId,
          puser: App.Global.OpenId,
        })
      },
      fnSucc: (resp) => {
        this.data.pid = resp.data.id
        wx.requestPayment({
          timeStamp: resp.data.wxmn.timeStamp,
          nonceStr: resp.data.wxmn.nonceStr,
          package: resp.data.wxmn.package,
          signType: resp.data.wxmn.signType,
          paySign: resp.data.wxmn.paySign,
          success(res) {
            console.log(res)
            this.execCheckPay()
          },
          fail(res) {
            console.log(res)
            App.doPost({
              uri: '/app/base/payment/cancel',
              body: {
                id: resp.data.id,
              }
            })
          }
        })
        // this.setData({
        //   show: true,
        //   pid: resp.data.id,
        //   qr: url,
        //   pamt: Kits.fmtMoneyString(resp.data.amount),
        //   pcnl: '微信'
        // })
        this.data.timer = setTimeout(() => {
          this.execCheckPay()
        }, 1000)
      },
    })
  },
  execCheckPay() {
    App.doPost({
      uri: '/app/base/payment/state',
      body: {
        id: this.data.pid,
      },
      fnSucc: resp => {
        if (resp.data.state == 1) {
          if (this.data.timer) {
            clearTimeout(this.data.timer)
          }
          this.data.timer = setTimeout(() => {
            this.execCheckPay()
          }, 1000)

          return
        }
        Util.Toast.clear()
        if (resp.data.state == 2) {
          Util.Toast.succ('用户已支付')
          this.setData({
            show: false,
            listShow: false
          })
        }
      },
    })
  },
  onLoad() {
    wx.sub('ReloadUserFin', this, () => {
      this.execLoadFin()
    })
    this.execLoadFin()
    this.getData()
    this.execIncomeLoad()
  },
  onUnload() {
    wx.cancel('ReloadUserFin', this)

  },
  execLoadFin() {
    App.doPost({
      uri: '/app/merchant/fin/summary',
      
      fnSucc: resp => {
        resp.data.couponStr = (resp.data.coupon) + ''
        resp.data.amountStr = Kits.fmtMoneyString(resp.data.amount)
        resp.data.settleStr = Kits.fmtMoneyString(resp.data.settle)
        resp.data.depositStr = Kits.fmtMoneyString(resp.data.deposit)
        this.setData({
          init: true,
          summ: resp.data
        })
      }
    })
  },
  toPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.uri
    })
  },

  // income
  doPulldown() {
    this.data.page = 1
    this.execIncomeLoad(true)
  },
  doReachBottom() {
    if (this.data.page >= this.data.pager.all) {
      return
    }
    this.data.page = this.data.page + 1
    this.execIncomeLoad(true)
  },
  doDateChange(e) {
    if (e.currentTarget.dataset.type == 'begin') {
      this.setData({
        begin: e.detail.value
      })
    } else {
      this.setData({
        close: e.detail.value
      })
    }
    this.execIncomeLoad(false)
  },
  execIncomeLoad(pulldown) {
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/merchant/income/list',
      body: {
        pager: {
          page: this.data.page,
          size: this.data.size,
          time: this.data.time
        },
        query: {
          begin: this.data.begin,
          close: this.data.close,
        }
      },
      fnSucc: resp => {
        if (resp.data.pager.now == 1) {
          this.data.time = resp.data.pager.fms
        }
        resp.data.items.forEach(item => {
          item.valueStr = Kits.fmtMoneyString(item.value)

        })

        this.setData({
          // init: true,
          loading: false,
          triggered: false,
          pager: resp.data.pager,
          items: resp.data.pager.now == 1 ? resp.data.items : this.data.items.concat(resp.data.items)
        })
      },
      fnComp: () => {
        if (pulldown) {
          this.triggerEvent('stopPullDown', {})
        }
      }
    })
  }
})